package jp.co.ysl.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import jp.co.ysl.service.Command;

public class DefaultInvocationHandler implements InvocationHandler {
	protected Object targetObject;

	public void setTargetObject(Object targetObject) {
		this.targetObject = targetObject;
	}

	@SuppressWarnings("finally")
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		try {
			System.out.println("----------------------------------");
			System.out.println("Method Call Start:[" + method + "]["
					+ System.currentTimeMillis() + "]");

			for(int i = 0; i < args.length; i++){
				Object obj = args[i];

				if(obj instanceof Command){
					System.out.println("----------------------------------");
					System.out.println("Param is Command :[" + method + "]["
							+ System.currentTimeMillis() + "]");
				}
			}

			return method.invoke(targetObject, args);
		} catch (Exception e) {
			System.out.println("----------------------------------");
			System.out.println("Exception Start:[" + method + "]["
					+ System.currentTimeMillis() + "]");
			e.printStackTrace();
		} finally {
			System.out.println("----------------------------------");
			System.out.println("Method Call End:[" + method + "]["
					+ System.currentTimeMillis() + "]");

			return null;
		}
	}
}
